package com.sise.stuteaintersys.controller;

import com.github.pagehelper.PageInfo;
import com.sise.stuteaintersys.bean.base.ModifyResponse;
import com.sise.stuteaintersys.bean.entity.*;
import com.sise.stuteaintersys.bean.protocol.NetResult;
import com.sise.stuteaintersys.bean.vo.*;
import com.sise.stuteaintersys.library.utils.FileUtils;
import com.sise.stuteaintersys.locallibs.enums.AdminEnum;
import com.sise.stuteaintersys.locallibs.enums.FileEnum;
import com.sise.stuteaintersys.locallibs.enums.ModifyEnum;
import com.sise.stuteaintersys.service.LoginService;
import com.sise.stuteaintersys.service.TeacherService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.Part;
import java.io.File;
import java.util.Collection;
import java.util.Date;
import java.util.List;

/**
 * @ClassName TeacherController
 * @Description
 * @Date 2019/1/3 16:30
 * @Version 1.0
 **/
@RestController
@Slf4j
@RequestMapping("teacher")
public class TeacherController {

    @Autowired
    LoginService loginService;

    @Autowired
    TeacherService teacherService;

    /**
     * 文件管理根目录
     **/
    @Value("${root.file}")
    private String root;

//登陆
    /**
     * 老师登陆验证
     * 
     *@param loginRequestVO
     *@return 
     *@dateTime 2019/1/8 14:56
     */
    @PostMapping(value = "/login/check")
    public LoginResponseVO checkLogin(LoginRequestVO loginRequestVO) {
        return loginService.checkTeacher(loginRequestVO);
    }

//课程

    /**
     *  获得某门课程内容
     * 
     *@param id
     *@return 
     *@dateTime 2019/1/8 14:57
     */
    @GetMapping(value = "/course/index")
    public NetResult getCourse(Integer id){
        return NetResult.okadddata(FileEnum.FILESUCCESS.getCode(),
                FileEnum.FILESUCCESS.getMessage(),
                teacherService.getCourseById(id));
    }

    /**
     *  已经结束的课程
     *
     *@param pageNum
     *@param pageSize
     *@param id 老师编号
     *@return
     *@dateTime 2019/1/8 15:15
     */
    @GetMapping(value = "/course/finished")
    public NetResult finishedCourse(Integer pageNum,Integer pageSize,Integer id){
        return NetResult.okadddata(FileEnum.FILESUCCESS.getCode(),
                FileEnum.FILESUCCESS.getMessage(),
                teacherService.finishedCourse(pageNum,pageSize,id));
    }

    /**
     *  正在上的课程
     *
     *@param pageNum
     *@param pageSize
     *@param id 老师编号
     *@return
     *@dateTime 2019/1/8 15:17
     */
    @GetMapping(value = "/course/list")
    public NetResult listCourse(Integer pageNum,Integer pageSize,Integer id) {
        return NetResult.okadddata(FileEnum.FILESUCCESS.getCode(),
                FileEnum.FILESUCCESS.getMessage(),
                teacherService.listCourse(pageNum,pageSize,id));
    }

    /**
     * 插入课程
     *
     * @param course
     * @return ModifyResopnse
     * @date 2019/1/4
     * @time 14:29
     */
    @PostMapping(value = "insert/course")
    public ModifyResponse insertCourse(Course course,@DateTimeFormat(pattern="yyyy-MM-dd") Date overDate) {
        course.setDeadline(overDate);
        return teacherService.insertCourse(course);
    }
    /**
     * 更新课程信息
     *
     *@param course
     *@param overDate
     *@return
     *@dateTime 2019/1/8 15:18
     */
    @PostMapping(value = "update/course")
    public ModifyResponse updateCourse(Course course,@DateTimeFormat(pattern="yyyy-MM-dd") Date overDate) {
        course.setDeadline(overDate);
        return teacherService.updateCourse(course);
    }
    /**
     *  删除课程
     *
     *@param id 课程编号
     *@return
     *@dateTime 2019/1/8 15:20
     */
    @PostMapping(value = "delete/course")
    public ModifyResponse deleteCourse(Integer id) {
        return teacherService.deleteCourse(id);
    }


//作业
    /**
     *  插入新的作业（一至多条）
     *
     *@param list
     *@return
     *@dateTime 2019/1/8 17:46
     */
    @PostMapping(value = "insert/homework")
    public ModifyResponse insertHomework(String list) {
        //String jsonString = "[{'title':'1+2','answer':'3','optionA':'9','optionB':'7','optionC':'3','optionD':'5','courseId':'1'}]";
        return teacherService.insertHomework(list);
    }

    /**
     * 课程的全部作业
     *
     *@param pageNum
     *@param pageSize
     *@param courseId
     *@return
     *@date 2019/1/6
     *@time 15:12
     */
    @GetMapping(value = "course/homework/list")
    public NetResult listHomework(Integer pageNum, Integer pageSize, Integer courseId){
        PageInfo<HomeworkDateVO> info = teacherService.listHomework(pageNum,pageSize,courseId);
        return NetResult.okadddata(FileEnum.FILESUCCESS.getCode(), FileEnum.FILESUCCESS.getMessage(), info);
    }
    /**
     *  一次作业的内容
     *
     *@param date
     *@return
     *@date 2019/1/6
     *@time 15:12
     */
    @GetMapping(value = "course/homework/content")
    public List<Homework> getHomework(@DateTimeFormat(pattern="yyyy-MM-dd") Date date,Integer courseId){
        List<Homework> list = teacherService.getHomework(date,courseId);
        return list;
    }

    /**
     *  更新一条作业的信息
     *
     *@param homework
     *@return
     *@dateTime 2019/1/8 17:49
     */
    @PostMapping(value = "update/homework")
    public ModifyResponse updateHomework(Homework homework) {
        return teacherService.updateHomework(homework);
    }
    /**
     *
     *  删除一条作业
     *
     *@param id
     *@return
     *@dateTime 2019/1/8 17:52
     */
    @PostMapping(value = "delete/homework")
    public ModifyResponse deleleHomework(Integer id) {
        return teacherService.deleteHomework(id);
    }
    /**
     * 删除某一天的作业
     *
     *@param
     *@return
     *@dateTime 2019/1/8 17:52
     */
    @PostMapping(value = "delete/homework/list")
    public ModifyResponse deleleListHomework(@DateTimeFormat(pattern="yyyy-MM-dd") Date date) {
        return teacherService.deleteListHomework(date);
    }
    /**
     *  查看作业完成情况
     *
     *@param
     *@return
     *@dateTime 2019/1/18 19:27
     */
    @GetMapping(value = "list/homework/result")
    public List<HomeworkResult> homeworkResults(Integer courseId)
    {
        return teacherService.listHomeworkResult(courseId);
    }
    /**
     *  某个作业的详情
     *
     *@param
     *@return
     *@dateTime 2019/1/18 19:27
     */
    @GetMapping(value = "list/homework/result/content")
    public HomeworkResultVO resultContent(@DateTimeFormat(pattern="yyyy-MM-dd") Date date,Integer courseId,Integer studentId){
        return teacherService.findResultContent(date,courseId,studentId);
    }

//试卷
    /**
     *  一键生成试卷
     *
     *@param
     *@return
     *@dateTime 2019/1/8 20:38
     */
    @PostMapping(value = "insert/exam")
    public ModifyResponse insertExam(ExamCourse examCourse) {
        return teacherService.insertExam(examCourse);
    }
    /**
     *  试卷列表
     *
     *@param
     *@return
     *@dateTime 2019/1/8 20:39
     */
    @GetMapping(value = "list/exam")
    public NetResult listExam(Integer pageNum, Integer pageSize, Integer courseId) {
        PageInfo<ExamCourse> info = teacherService.listExam(pageNum,pageSize, courseId);
        return NetResult.okadddata(FileEnum.FILESUCCESS.getCode(), FileEnum.FILESUCCESS.getMessage(), info);
    }
    /**
     *  试卷内容
     *
     *@param
     *@return
     *@dateTime 2019/1/8 20:39
     */
    @GetMapping(value = "list/question")
    public NetResult listQuestion(String examType,String courseId) {
        List<Question> info = teacherService.listQuestion(examType,courseId);
        return NetResult.okadddata(FileEnum.FILESUCCESS.getCode(), FileEnum.FILESUCCESS.getMessage(), info);
    }
    @PostMapping(value = "delete/exam")
    public ModifyResponse deleteExam(ExamCourse course){
        return teacherService.deleteExam(course);
    }
    /**
     *  返回试卷完成情况
     *
     *@param
     *@return
     *@dateTime 2019/1/18 18:48
     */
    @GetMapping(value = "list/exam/result")
    public List<ExamResult> resultExam(Integer courseId){
        return  teacherService.listResult(courseId);
    }
    /**
     *  某次考试的详细情况
     *
     *@param
     *@return
     *@dateTime 2019/1/18 19:49
     */
    @GetMapping(value = "list/exam/result/content")
    public ExamResultVO resultContent(String examType, Integer courseId, Integer studentId){
            return teacherService.findExamResult(examType, courseId,  studentId);
    }
//留言
    /**
     *  留言板列表
     *
     *@param
     *@return
     *@dateTime 2019/1/8 20:39
     */
    @GetMapping(value = "course/message")
    public NetResult listMessage(Integer pageNum, Integer pageSize,Integer id) {
        return NetResult.okadddata(ModifyEnum.MESSAGESELETESUCCESS.getCode(),
                ModifyEnum.MESSAGESELETESUCCESS.getMsg(),
                teacherService.listMessage(pageNum, pageSize, id));
    }
    /**
     *  回复留言
     *
     *@param
     *@return
     *@dateTime 2019/1/8 20:40
     */
    @PostMapping(value = "/course/message/response")
    public NetResult responseMessage(Message message) {
        return teacherService.responseMessage(message);
    }

    /**
     * 专业方向
     */
    @GetMapping(value = "profession")
    public List<ProfessionType> professionTypes(){
        return teacherService.listProfession();
    }

    /**
     * 导入Excel表格的question
     * @param destination
     * @param fileName
     * @param request
     * @return
     */
    @PostMapping(value = "batchImport")
    public NetResult batchImportExam(@RequestParam("destination") String destination,
                                     @RequestParam("fileName") String fileName,
                                     @RequestParam("courseId") Integer courseId,
                                     HttpServletRequest request){
        log.info("destination:"+destination);
        log.info("fileName:"+fileName);
        log.info("courseId:"+courseId);
        try {
            Collection<Part> parts = request.getParts();
            for (Part part : parts) {
                if (part.getContentType() != null) {
                    // 忽略路径字段,只处理文件类型
                    String path = root + destination;
                    File f = new File(path, FileUtils.getFileName(part.getHeader("content-disposition")));
                    if (!FileUtils.write(part.getInputStream(), f)) {
                        throw new Exception("文件上传失败");
                    }
                }
            }
            File file = new File(root, "/"+fileName);
            if(teacherService.saveExcelInfo(file, courseId)){
                return NetResult.netresultok();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return NetResult.error(AdminEnum.EXCELLUPLOADERROR.getCode(), AdminEnum.EXCELLUPLOADERROR.getMessage());
    }
}
